package br.com.fsa.aplication.Services;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.fsa.aplication.Model.UsuarioVO;

public class AutenticarLoginFilter implements Filter
{
	private FilterConfig config = null;

	@Override
	public void destroy()
	{
		config = null;		
	}

	@Override
	public void doFilter(ServletRequest _request, ServletResponse _response,
			FilterChain chain) throws IOException, ServletException
	{
		HttpSession sessao =  (( HttpServletRequest)_request).getSession();
		HttpServletRequest httpReq = (HttpServletRequest)_request;
		
			
		StringBuffer url =  httpReq.getRequestURL();
		
		System.out.println(url.toString());
		
		UsuarioVO login = (UsuarioVO)sessao.getAttribute("USUARIO");
		
		if(login == null )
		{
			((HttpServletResponse) _response ).sendRedirect("../Login.xhtml");
		}
		else
		{
		//	((HttpServletResponse) _response ).sendRedirect(url.toString());
			chain.doFilter(_request, _response);
		}
		
	}

	@Override
	public void init(FilterConfig filter) throws ServletException
	{
		config = filter;
	}

}
